package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.domain.UserChat;
import cn.wolfcode.trip.base.query.StrategyCommentQueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.IStrategyCommentService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.service.IUserChatService;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/userChats")
public class UserChatController {
    @Autowired
    IUserChatService userChatService;
    @Autowired
    IUserService userService;

    //查询所有用户用来回显回话列表
    @GetMapping("{myUserId}")
    public List<UserChat> getAllUsers(@PathVariable Long myUserId){
        //查询所有接收我消息的用户
        List<UserChat> userChats = userChatService.getAllUsers(myUserId);
        //查询所有发我消息的用户
      /* List<UserChat> list=userChatService.getAllSendUsers(myUserId);
        *//* for (UserChat userChat : list) {
            System.out.println(userChat.getMyUser().getNickName());
            User user1 = userChat.getMyUser();
            User user2 = userChat.getAcceptUser();
            userChat.setAcceptUser(user1);
            userChat.setMyUser(user2);

               *//**//* UserChat chat =new UserChat();
                Long acceptId=userChat.getMyUser().getId();
                Long myId=userChat.getAcceptUser().getId();
                chat.setAcceptUser(userService.queryUserById(acceptId));
                chat.setMyUser(userService.queryUserById(myId));
                userChats.add(chat);*//**//*
        }*//*
        userChats.addAll(list);*/
        return userChats;
    }
    //查询当前正在聊天的对象的信息
    @GetMapping
    public UserChat getAcceptUser(Long acceptUserId){
        return userChatService.getAcceptUser(acceptUserId);
    }
    //获取聊天记录
    @GetMapping("/messages")
    public List<UserChat> getMessages(Long sendId,Long currentId){
        return userChatService.getMessages(sendId,currentId);
    }
    //保存聊天记录
    @PostMapping
    public JSONResult saveMessage(UserChat userChat){
        userChatService.saveMessage(userChat);
        return new JSONResult();
    }
    //查询最新的一条数据
    @GetMapping("/newMessages")
    public List<UserChat> getNewMessages(Long sendId,Long currentId){
        return userChatService.getNewMessages(sendId,currentId);
    }
    //查询数据库总数
    @GetMapping("/counts")
    public Integer getCounts(Long sendId,Long currentId){
        return userChatService.getCounts(sendId,currentId);
    }
}
